package com.cuanbobox.base;

import android.Manifest;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import com.cuanbobox.bean.AtyInfo;
import com.cuanbobox.service.BackgroundService;
import com.cuanbobox.utils.ActivityArgumentsUtil;
import com.cuanbobox.utils.ActivityViewUtil;
import com.cuanbobox.utils.AppUtil;
import com.cuanbobox.utils.LogUtil;
import com.cuanbobox.utils.Permission;
import com.cuanbobox.utils.SharedPreferencesUtil;
import com.google.gson.reflect.TypeToken;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class LHBaseActivity extends AppCompatActivity {
    private View rtView;
    private boolean isFirst = true;
    protected App app;
    private ActivityArgumentsUtil activityArgumentsUtil = new ActivityArgumentsUtil(this);
    private ActivityViewUtil activityViewUtil;
    private AtyInfo atyInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getResources();
        hideBottomUIMenu();
        atyInfo = new AtyInfo(this, AtyInfo.STATE_CREATE);
        App.getInstance().addAty(atyInfo);
        app = (App) getApplication();
        //申请权限
        Permission.getInstance()
                .add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .add(Manifest.permission.READ_EXTERNAL_STORAGE)
                .add(Manifest.permission.READ_CONTACTS)
                .add(Manifest.permission.FOREGROUND_SERVICE)
                .add(Manifest.permission.ACCESS_WIFI_STATE)
                .add(Manifest.permission.SYSTEM_ALERT_WINDOW)
                .add(Manifest.permission.RECEIVE_BOOT_COMPLETED)
                .add(Manifest.permission.REQUEST_INSTALL_PACKAGES)
                .add(Manifest.permission.QUERY_ALL_PACKAGES)
                .add(Manifest.permission.RECORD_AUDIO)
                .add(Manifest.permission.CHANGE_NETWORK_STATE)
                .add(Manifest.permission.WRITE_SETTINGS)
                .add("android.permission.READ_MEDIA_IMAGES")
                .add("android.permission.READ_MEDIA_AUDIO")
                .add("android.permission.READ_MEDIA_VIDEO")
                .add("android.permission.POST_NOTIFICATIONS")
                /*.add(Manifest.permission.READ_MEDIA_IMAGES)
                .add(Manifest.permission.READ_MEDIA_AUDIO)
                .add(Manifest.permission.READ_MEDIA_VIDEO)
                .add(Manifest.permission.POST_NOTIFICATIONS)*/
                .request(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        atyInfo.setState(AtyInfo.STATE_RESUME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        atyInfo.setState(AtyInfo.STATE_PAUSE);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        rtView = view;
        activityViewUtil = new ActivityViewUtil(rtView);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (isFirst) {
            isFirst = false;
            requestData();
        }
    }

    /**
     * 在这里做http请求
     */
    public void requestData() {

    }

    @Override
    protected void onStop() {
        super.onStop();
        atyInfo.setState(AtyInfo.STATE_STOP);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        atyInfo.setState(AtyInfo.STATE_DESTROY);
        App.getInstance().removeAty(atyInfo);
    }

    /**
     * 启用所有View
     */
    public void enable() {
        activityViewUtil.enable();
    }

    /**
     * 禁用所有View
     */
    public void disable(View[] aryView) {
        activityViewUtil.disable(aryView);
    }

    /**
     * 获取传过来的String参数
     *
     * @param key 键
     */
    public String getArguments(String key) {
        return activityArgumentsUtil.getStringArguments(key);
    }

    /**
     * 获取传过来的boolean参数
     *
     * @param key          键
     * @param defaultValue 默认值
     */
    public boolean getArguments(String key, boolean defaultValue) {
        return activityArgumentsUtil.getBooleanArguments(key, defaultValue);
    }

    /**
     * 获取传过来的int参数
     *
     * @param key          键
     * @param defaultValue 默认值
     */
    public int getArguments(String key, int defaultValue) {
        return activityArgumentsUtil.getIntArguments(key, defaultValue);
    }

    /**
     * 获取传过来的double参数
     *
     * @param key          键
     * @param defaultValue 默认值
     */
    public double getArguments(String key, double defaultValue) {
        return activityArgumentsUtil.getDoubleArguments(key, defaultValue);
    }

    /**
     * 获取传过来的float参数
     *
     * @param key          键
     * @param defaultValue 默认值
     */
    public float getArguments(String key, float defaultValue) {
        return activityArgumentsUtil.getFloatArguments(key, defaultValue);
    }

    /**
     * 获取传过来的实体对象参数
     *
     * @param cls 传递过来的实体对象类型
     */
    public Object getArguments(Class cls) {
        return activityArgumentsUtil.getObjectArguments(cls, cls.getSimpleName());
    }

    /**
     * 获取传过来的实体对象参数
     *
     * @param cls 传递过来的实体对象类型
     * @param key 键
     */
    public Object getArguments(Class cls, String key) {
        return activityArgumentsUtil.getObjectArguments(cls, key);
    }

    /**
     * 获取传过来的List集合参数
     *
     * @param key       键
     * @param typeToken 要解析的集合数据类型
     */
    public List<?> getArguments(String key, TypeToken typeToken) {
        return activityArgumentsUtil.getListArguments(key, typeToken);
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            Window _window = getWindow();
            WindowManager.LayoutParams params = _window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
            _window.setAttributes(params);
        }
    }
}
